﻿using Microsoft.Web.Services3.Security.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Wicresoft.EKA.AtriumWebService
{
    class ClientMessageInspector : IClientMessageInspector
    {

        private const string USER_AGENT_HTTP_HEADER = "user-agent";

        private string m_userAgent;

        public ClientMessageInspector(string userAgent)
        {
            this.m_userAgent = userAgent;
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {

        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            //request.Headers.Add(MessageHeader.CreateHeader("UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", new UsernameToken()
            //        {
            //            Username = "Demo",
            //            Password = "Pass01!"
            //        }
            //    ));

            //Soap

            //request.Headers.Add(MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", new AtriumSecurity()
            //{
            //    UsernameToken = new UsernameToken()
            //    {
            //        Username = "Demo",
            //        Password = "Pass01!!",
            //        Nonce = Guid.NewGuid().ToString(),
            //        Created = DateTime.Now.AddHours(1)
            //    }
            //}, true));

            //request.Headers.Add(MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", new Security()
            //{
            //    usernameToken = new UsernameToken()
            //      {
            //          username = "Demo",
            //          password = "Pass01!",
            //          //Username = "admin",
            //          //Password = "1Q2w3e4r",
            //          nonce = Guid.NewGuid().ToString(),
            //          //created = DateTime.Now.AddHours(1)

            //      }
            //}));

            // ut = new UsernameToken("DemoInit", "DemoInit", PasswordOption.SendPlainText);

            UsernameToken ut = new UsernameToken("Demo", "Pass01!", PasswordOption.SendPlainText);

            XmlElement securityElement = ut.GetXml(new XmlDocument());

            MessageHeader myHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", securityElement, false);
            request.Headers.Add(myHeader);

//            request.Headers.Add(MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", XMLHelper.GetXML(),false));

////      string x =  " <wsse:Security soapenv:mustUnderstand=""1"" xmlns:wsse=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">"+
////"<wsse:UsernameToken>"+
////"<wsse:Username>user</wsse:Username>"+
////"<wsse:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"">pass</wsse:Password>"+
//// "</wsse:UsernameToken>"+
////"</wsse:Security>";

//            request.Headers.Add(MessageHeader.CreateHeader("test", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", new LoginInfoTest()
//                {
//                    authentication = "Test"
//                }, new DataContractSerializer(typeof(LoginInfoTest)), true));

            //request.Headers.Add(MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", soap new UsernameToken()
            //{
            //    Username = "admin",
            //    Password = "1Q2w3e4r",
            //    Nonce = Guid.NewGuid().ToString(),
            //    Created = DateTime.Now.AddHours(1)

            //}, true));


            //request.Headers.Add(MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", new AtriumSecurity()
            //{
            //    UsernameToken = new UsernameToken()
            //    {
            //        Username = "Demo",
            //        Password = "Pass01!!",
            //        Nonce = Guid.NewGuid().ToString(),
            //        Created = DateTime.Now.AddHours(1)
            //    }
            //}, new DataContractSerializer(typeof(AtriumSecurity)), true));

            //request.Headers.Add(MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", 
            //   @"<wsse:UsernameToken><wsse:Username>Demo</wsse:Username><wsse:PasswordType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">"
            //     +  @"<wsse:Nonce>LyEQw+0pSZr+a+CJVSuNgOvp9cY=</wsse:Nonce><wsu:Created>2009-01-25T11:45:30Z</wsu:Created></wsse:UsernameToken>"));

            HttpRequestMessageProperty httpRequestMessage;
            object httpRequestMessageObject;
            if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
            {
                httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;

                httpRequestMessage.Headers["User"] = "Pass01!";

            }
            else
            {
                httpRequestMessage = new HttpRequestMessageProperty();
                httpRequestMessage.Headers["User"] = "Pass01!";
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
            }
            return null;


            return null;
        }
    }
}
